<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增或编辑账目</title>
    <link th:replace="template/sys_header :: header"/>
</head>
<body>
<form lay-filter="form1" class="layui-form margintop20" action="">
    <input type="hidden" id="cashId" name="cashId" value=""/>
    <input type="hidden" id="dictId" name="dictId" value=""/>
    <input type="hidden" id="cashCategory" name="cashCategory" value=""/>
    <div class="layui-row margintop15">
        <div class="layui-col-sm2 layui-row-label">
            <span class="x-red">*</span>入账类型
        </div>
        <div class="layui-col-sm5">
            <select name="typeSelect" id="typeSelect" lay-verify="required" lay-search>
                <option value="0">支出</option>
                <option value="1">收入</option>
                <option value="2">笔记</option>
            </select>
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            <span class="x-red">*</span>入账时间
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="recordTime" name="recordTime" lay-verify="required" autocomplete=""
                   class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux" style="margin-left: 5px;">
            <span class="x-red">*</span>请输入记录入账时间
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            <span class="x-red">*</span>入账类别
        </div>
        <div class="layui-col-sm5">
            <!--单选select-->
            <select name="dictSelect" id="dictSelect" lay-verify="required" lay-search>
                <!--<option value="">请选择或搜索选择</option>
                <option value="1">layer</option>
                <option value="2">form</option>-->
            </select>
        </div>
    </div>
    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            <span class="x-red">*</span>账目金额
        </div>
        <div class="layui-col-sm5">
            <input type="text" id="amount" name="amount" lay-verify="required|number" autocomplete=""
                   class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux" style="margin-left: 5px;">
            <span class="x-red">*</span>单位元
        </div>
    </div>

    <div class="layui-row margintop10">
        <div class="layui-col-sm2 layui-row-label">
            备注
        </div>
        <div class="layui-col-sm5">
            <textarea id="remark" name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-row margintop15">
        <div class="layui-col-sm2 layui-row-label">

        </div>
        <div class="layui-col-sm5" style="text-align: center">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save"><i class="iconfont">&#xe747;</i>
                &nbsp;保存&nbsp;
            </button>
            &nbsp;&nbsp;
            <button class="layui-btn layui-btn-danger" onclick="xadmin.close()"><i class="iconfont">&#xe747;</i>
                &nbsp;关闭&nbsp;
            </button>
        </div>
    </div>
</form>
<script type="text/javascript" th:replace="template/sys_header :: footer"></script>
<script type="text/javascript" th:inline="javascript">
    var $ = layui.jquery, form = layui.form, laydate = layui.laydate;
    var cashbook = [[${cashbook}]];//角色信息
    var isEdit = false;//默认不是编辑模式
    console.log(cashbook);
    if (cashbook && cashbook.cashId) {
        isEdit = true;//编辑
        form.val('form1', {
            "cashId": cashbook.cashId,
            "cashType": cashbook.cashType,
            "recordTime": cashbook.recordTime,
            "dictId": cashbook.dictId,
            "cashCategory": cashbook.cashCategory,
            "amount": cashbook.amount,
            "remark": cashbook.remark
        });
        getDictByPId("649036610398617600", "dictSelect", cashbook.dictId);
        $("#typeSelect").val(cashbook.cashType);
    } else {
        getById("recordTime").value = new Date().Format("yyyy-MM-dd");
        getDictByPId("649036610398617600", "dictSelect");
    }

    $(function () {
        //执行一个laydate实例
        laydate.render({
            elem: '#recordTime' //指定元素
        });

        form.on('submit(save)', function (data) {
            //把选择的数据字典保存到对应字段中
            var selectDictId = $("#dictSelect").val();
            var selectCashCategory = $("#dictSelect").find("option:selected").text();
            data.field.dictId = selectDictId;
            data.field.cashCategory = selectCashCategory;
            var selectCashType = $("#typeSelect").val();
            data.field.cashType = selectCashType;
            //console.log(data.field);
            //return false;
            //发异步，数据提交
            if (isEdit) {
                edit(data.field);
            } else {
                add(data.field);
            }
            return false;
        });
    })

    function add(field) {
        new AjaxRequest({
            type: "POST",
            url: webroot + "bus/cashbook/add",
            data: JSON.stringify(field),
            isShowLoader: true,
            success: function (res) {
                //console.log(res);
                if (res) {
                    if (res.resultCode == "0000") {
                        layer.alert("保存成功", {icon: 1}, function () {
                            //xadmin.father_reload();//刷新父窗体
                            xadmin.getParentWindow().refreshlist();
                            xadmin.close();//关闭当前frame
                        });
                    } else {
                        layer.alert(res.resultMessage, {icon: 2})
                    }
                }
            }
        });
    }

    function edit(field) {
        new AjaxRequest({
            type: "POST",
            url: webroot + "bus/cashbook/edit",
            data: JSON.stringify(field),
            isShowLoader: true,
            success: function (res) {
                //console.log(res);
                if (res) {
                    if (res.resultCode == "0000") {
                        layer.alert("保存成功", {icon: 1}, function () {
                            //xadmin.father_reload();//刷新父窗体
                            xadmin.getParentWindow().refreshlist();
                            xadmin.close();//关闭当前frame
                        });
                    } else {
                        layer.alert(res.resultMessage, {icon: 2})
                    }
                }
            }
        });
    }
</script>
</body>
</html>